﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.UI;
using WorseGame.Common.Cores.Custom.Structs;
using WorseGame.Common.Players;
using WorseGame.Common.UIs.Elements;

namespace WorseGame.Common.UIs
{

    public class FishingState : UIState
    {
        public static readonly SoundStyle EnterBarSound = new(nameof(WorseGame) + "/Assets/Sounds/EnterFishBar");
        public static readonly SoundStyle ExitBarSound = new(nameof(WorseGame) + "/Assets/Sounds/ExitFishBar");
        public UIImage backdrop;
        public UIImage fishzone;
        public VerticalProgressBar catchProgressBar;
        public PlayerFishBar playerCatchBar;
        public FishIcon fishIcon;
        public CrateIcon crateIcon;
        private readonly float _startingProgressPercent = 0.234f;
        public FishingState(CatchData catchData, Player catchingPlayer)
        {
            Texture2D backdropTexture = ModContent.Request<Texture2D>("WorseGame/Assets/Textures/UIs/MiniGameBackGround", ReLogic.Content.AssetRequestMode.ImmediateLoad).Value;
            backdrop = new UIImage(backdropTexture)
            {
                VAlign = 0.5f,
                HAlign = 0.5f,
                Color = Color.White * 0.8f
            };
            backdrop.Width.Set(104f, 0f);
            backdrop.Height.Set(314f, 0f);
            Append(backdrop);
            Texture2D fishZoneTexture = ModContent.Request<Texture2D>("WorseGame/Assets/Textures/UIs/MiniGameCursor", ReLogic.Content.AssetRequestMode.ImmediateLoad).Value;
            fishzone = new UIImage(fishZoneTexture)
            {
                VAlign = 0.5f,
                HAlign = 0.5f
            };
            fishzone.Width.Set(104f, 0f);
            fishzone.Height.Set(314f, 0f);
            fishzone.Left.Set(-10f, 0f);
            Append(fishzone);

            catchProgressBar = new VerticalProgressBar(_startingProgressPercent)
            {
                VAlign = 0.5f,
                HAlign = 0.5f
            };
            catchProgressBar.Left.Set(30f, 0f);
            catchProgressBar.Width.Set(8f, 0f);
            catchProgressBar.Height.Set(288f, 0f);
            fishzone.Append(catchProgressBar);

            float playerFishingSkill = catchingPlayer.GetFishingConditions().FinalFishingLevel;
            playerCatchBar = new PlayerFishBar
            {
                Width = new StyleDimension(18f, 0f),
            };
            playerCatchBar.Height.Set(0f, MathHelper.Clamp(playerFishingSkill / 300f * 0.9f, 0.175f, 0.55f));
         
            fishzone.Append(playerCatchBar);

            fishIcon = new FishIcon(catchData)
            {
                HAlign = 0.5f,
                VAlign = 0.95f
            };
            fishzone.Append(fishIcon);        
            if (Main.rand.NextFloat() <= playerFishingSkill / 305f + (catchingPlayer.cratePotion ? 0.2f : 0f))
            {
                crateIcon = new CrateIcon
                {
                    VAlign = Main.rand.NextFloat(0.25f, 0.55f),
                    HAlign = 0.5f,
                    isAppearing = true,
                    iconScale = 0f
                };
                fishzone.Append(crateIcon);

                SoundEngine.PlaySound(SoundID.Grab);
            }
            OnLeftMouseDown += delegate { playerCatchBar.playerHoldingDownLMB = true; };
            OnLeftMouseUp += delegate { playerCatchBar.playerHoldingDownLMB = false; };
        }

        public override void Update(GameTime gameTime)
        {
            if (!Main.hasFocus && Main.netMode == NetmodeID.SinglePlayer)
            {
                return;
            }
            base.Update(gameTime);
            CalculatedStyle barStyle = playerCatchBar.GetDimensions();
            CalculatedStyle fishStyle = fishIcon.GetDimensions();
            float barTop = barStyle.Y;
            float barBottom = barStyle.Y + barStyle.Height;
            float fishTop = fishStyle.Y;
            float fishBottom = fishStyle.Y + fishStyle.Height;
            bool barContainsFish = (fishTop <= barBottom && fishBottom >= barTop);

            if (barContainsFish && !playerCatchBar.barOverFish)
            {
                SoundEngine.PlaySound(EnterBarSound);
            }
            else if (!barContainsFish && playerCatchBar.barOverFish)
            {
                SoundEngine.PlaySound(ExitBarSound);
            }

            playerCatchBar.barOverFish = fishIcon.shouldShake = barContainsFish;

            if (crateIcon is not null)
            {
                CalculatedStyle crateStyle = crateIcon.GetDimensions();
                float crateTop = crateStyle.Y;
                float crateBottom = crateStyle.Y + crateStyle.Height;
                crateIcon.barOverCrate = (crateTop <= barBottom && crateBottom >= barTop);
            }

            UpdateProgressBar(barContainsFish);
        }

        private void UpdateProgressBar(bool barContainsFish)
        {

            float positiveRateOfChange = 0.1225f / 60f;

            float negativeRateOfChange = -0.1435f / 60f;

            catchProgressBar.percentProgress += barContainsFish ? positiveRateOfChange : negativeRateOfChange;

            FishingPlayer fishingPlayer = Main.LocalPlayer.GetModPlayer<FishingPlayer>();
            if (catchProgressBar.percentProgress >= 1f)
            {
                fishingPlayer.FinishCatchingFish(true, crateIcon?.crateCaught ?? false);
            }
            else if (catchProgressBar.percentProgress <= 0f)
            {
                fishingPlayer.FinishCatchingFish(false, false);


                SoundEngine.PlaySound(SoundID.Item130);
            }
        }
    }
}